package com.example.front.controller;

import com.example.gok_mall.entity.User;
import com.example.gok_mall.reponseData.ResponseCode;
import com.example.gok_mall.reponseData.ResultData;
import com.example.front.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
@ResponseBody
@Api(value = "前台用户接口类")
public class UserController {
    @Autowired
    private UserService userService;

    private ResultData resultData;

    @ApiOperation(value = "前台登录的方法", notes = "用户根据用户名和密码进行登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", dataType = "zhangsan", paramType = "body"),
            @ApiImplicitParam(name = "password", value = "用密码", dataType = "123456", paramType = "body")
    })
    //实现用户登录
    @PostMapping("/login")
    public ResultData login(String username, String password, HttpSession session) {
        ResultData resultData = userService.queryUserByUsernameAndPassword(username, password);

        //登陆成功则保存用户信息到session
        if (resultData.getCode() == ResponseCode.SUCCESS.getCode()) {
            session.setAttribute("userInfo", resultData.getData());
        }
        return resultData;

    }

    //获取用户信息
    @GetMapping("/get_user_info")
    public ResultData getUserInfo(HttpSession session) {
        User userInfo = (User) session.getAttribute("userInfo");
        if (userInfo == null) {
            return ResultData.fail("对不起，你没有登陆，请登录后在尝试");
        }
        return ResultData.createData(userInfo);
    }

    //用户数据更新
    @PutMapping("/update_information")
    public ResultData updateUser(HttpSession session, User user) {
        //从session中获取用户的数据
        //username是不能被更新的
        //email也要进行校验，校验新的email是不是已经存在，如果当存在相同的email的话，不能是我们当前的用户的
        User userInfo = (User) session.getAttribute("userInfo");
        if (userInfo == null) {
            return ResultData.fail("对不起，你没有登陆，请登录后在尝试");
        }
        //设置id
        user.setId(userInfo.getId());
        // String 类型的值比较 使用equals
        String success_code = ResponseCode.SUCCESS.getCode();
        if (userService.updateUser(user).getCode().equals(success_code)) {
            //需要将session中的用户信息更新

            session.setAttribute("userInfo", "新用户数据");
            return ResultData.success("更新成功");
        } else {
            return ResultData.fail("更新失败");
        }

    }

    //用户退出
    @GetMapping("/logout")
    public ResultData logout(HttpSession session) {
        //退出登录则清除session中的用户信息
        session.removeAttribute("userInfo");
        return ResultData.success("退出成功");

    }
    //用户找回密码，
    @GetMapping("/forget_get_question.do")
    public ResultData getUserPasswordQuestion(String username) {
        return userService.queryUserPasswordQuestion(username);
    }

    //用户找回密码
    @GetMapping("/forget_check_answer.do")
    public ResultData checkPasswordAnswer(String username, String question, String answer) {
        return userService.checkUserPasswordAnswer(username, question, answer);
    }

    //用户重重置自己的密码
    @PostMapping("/forget_reset_password.do")
    public ResultData resetPasswordByToken(String username,String password,String forgetToken){
        return userService.resetPasswordByToken(username,password,forgetToken);
    }

    //注册新用户
    @PostMapping("/adduser")
    public ResultData addUser(User user) {
            return userService.addUser(user);
        }
    //检查用户是否符合要求（用户名不重复）
    @GetMapping("/checkUsername")
    public ResultData checkValid(String str,String type) {
        return userService.checkValid(str,type);
    }

    //登录用户修改密码
    @PostMapping("/reset_password.do")
    public ResultData resetPassword(String passwordOld,String passwordNew,User user) {
        return  userService.resetPassword(passwordOld,passwordNew,user);
    }
}

